package com.xinchuang.mapper.targetSystem;

import com.xinchuang.entity.targetSystem.system.SystemsRelationship;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @ClassName SystemsRelationshipMapper
 * @Description: 体系关系表
 * @auther xukl
 * @data 2021/3/31  11:12
 * @Version V1.0
 */
public interface SystemsRelationshipMapper {


    @Insert(value = {
            "INSERT INTO systems_relationship ",
            "(sys_ship_id,sys_id,target_one,target_two,point,relation_type) ",
            "VALUES ",
            "(#{sysShipId},#{sysId},#{targetOne},#{targetTwo},#{point},#{relationType})",
            "ON DUPLICATE KEY UPDATE sys_id = #{sysId}, target_one = #{targetOne}, target_two = #{targetTwo}, point = #{point}, relation_type = #{relationType}"
    })
    int save(SystemsRelationship systemsRelationship);

    @Delete(value = "DELETE FROM systems_relationship WHERE sys_id = #{sysId}")
    int deleteBySysId(String sysId);

    @Select(value = {
            "SELECT sr.sys_ship_id,sr.sys_id,sr.target_one,sr.target_two,sr.point,sc.name AS relation_type ",
            "FROM systems_relationship AS sr LEFT JOIN system_config AS sc ON sr.relation_type = sc.id ",
            "WHERE sr.sys_id = #{sysId} "
    })
    @Results(id = "commonResultMap", value = {
            @Result(property = "sysShipId", column = "sys_ship_id"),
            @Result(property = "sysId", column = "sys_id"),
            @Result(property = "targetOne", column = "target_one"),
            @Result(property = "targetTwo", column = "target_two"),
            @Result(property = "point", column = "point"),
            @Result(property = "relationType", column = "relation_type"),
    })
    List<SystemsRelationship> findBySysId(String sysId);

    @Select(value = {
            "SELECT sr.sys_ship_id,sr.sys_id,sr.target_one,sr.target_two,sr.point,sc.name AS relation_type ",
            "FROM systems_relationship AS sr LEFT JOIN system_config AS sc ON sr.relation_type = sc.id ",
            "WHERE sr.sys_id = #{sysId} AND sr.target_one = #{targetId}"
    })
    @ResultMap(value = "commonResultMap")
    List<SystemsRelationship> findBySysIdAndTargetOne(String sysId, String targetId);

    @Select(value = {
            "SELECT sr.sys_ship_id,sr.sys_id,sr.target_one,sr.target_two,sr.point,sc.name AS relation_type ",
            "FROM systems_relationship AS sr LEFT JOIN system_config AS sc ON sr.relation_type = sc.id ",
            "WHERE sr.sys_id = #{sysId} AND sr.target_one IN (SELECT target_id FROM systems_relation WHERE sys_id = #{sysId} AND target_type = 1)"
    })
    @ResultMap(value = "commonResultMap")
    List<SystemsRelationship> findBySysIdAndSystemElse(String sysId);

    @Select(value = {
            "SELECT sr.sys_ship_id,sr.sys_id,sr.target_one,sr.target_two,sr.point,sc.name AS relation_type ",
            "FROM systems_relationship AS sr LEFT JOIN system_config AS sc ON sr.relation_type = sc.id ",
            "WHERE sr.sys_id = #{sysId} AND sr.target_one IN (SELECT target_id FROM system_relation WHERE s_id = #{sId})"
    })
    @ResultMap(value = "commonResultMap")
    List<SystemsRelationship> findBySysIdAndSystem(String sysId, String sId);

    @Select(value = {
            "SELECT sys_ship_id,sys_id,target_one,target_two,point,relation_type FROM systems_relationship WHERE sys_ship_id = #{sysShipId} "
    })
    @Results(value = {
            @Result(property = "sysShipId", column = "sys_ship_id"),
            @Result(property = "sysId", column = "sys_id"),
            @Result(property = "targetOne", column = "target_one"),
            @Result(property = "targetTwo", column = "target_two"),
            @Result(property = "point", column = "point"),
            @Result(property = "relationType", column = "relation_type"),
    })
    SystemsRelationship findById(String sysShipId);

    @Update(value = {
            "<script>",
            "UPDATE systems_relationship ",
            "<set>",
            "<if test = 'targetOne != null'>target_one = #{targetOne},</if>",
            "<if test = 'targetTwo != null'>target_two = #{targetTwo},</if>",
            "<if test = 'point != null'>point = #{point},</if>",
            "<if test = 'relationType != null'>relation_type = #{relationType},</if>",
            "</set>",
            "WHERE sys_ship_id = #{sysShipId}",
            "</script>"
    })
    int update(SystemsRelationship systemsRelationship);

    @Delete(value = "DELETE FROM systems_relationship WHERE sys_ship_id = #{sysShipId}")
    int deleteBySysShipId(String sysShipId);

    @Delete(value = "DELETE FROM systems_relationship WHERE sys_id = #{sysId} AND (target_one = #{targetId} OR target_two = #{targetId})")
    int deleteShipOneTargetId(@Param("sysId") String sysId, @Param("targetId") String targetId);

    @Delete(value = "DELETE FROM systems_relationship WHERE (target_one = #{targetId} OR target_two = #{targetId})")
    int deleteShipTargetId( @Param("targetId") String targetId);

}


